package util;

import generic.Person;
import generic.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/*
* Collection父接口: 单列集合顶级父接口
* Collections工具类: 专门用来操作集合的工具类
*
*   public static <T> boolean addAll(Collection<? super T> c, T... elements):将指定的元素添加到指定的集合
*   ? super E : 泛型下限 作用: 限制传入的泛型只能为E类型或E的父类型
*
*   public static void reverse(List<?> list):反转制定列表中的元素顺序
*
*   public static void shuffle(List<?> list):可以随机集合中的元素
* */
public class CollectionsDemo01 {
    public static void main(String[] args) {
        //method01();
        //method02();
        //method03();
        ArrayList<String> al = new ArrayList<>(Arrays.asList("张三","李四","王五"));
        Collections.shuffle(al);//每次调用相当于对集合中的元素随机排列
        System.out.println(al);
        System.out.println(al.get(0));
    }

    private static void method03() {
        ArrayList<String> al = new ArrayList<>(Arrays.asList("张三","李四","王五"));
        System.out.println(al);
        System.out.println("==============");
        Collections.reverse(al);
        System.out.println("反转后的列表为" + al);
    }

    private static void method02() {
        //简而言之: 利用addAll将指定的元素添加到指定集合,该集合既可以为元素自身类型,也可以为元素的父类型(可以兼容)
        ArrayList<Person> people = new ArrayList<>();
        ArrayList<Student> students = new ArrayList<>();
        Collections.addAll(students, new Student("流川枫"));// T为Student  ? super T => ? super Student,此时集合可以传入Collection<Student>,Collection<Person>
        //addAll()相当于
        // ArrayList<Student> students = new ArrayList<>();
        //students.add(new Student("流川枫"))

        Collections.addAll(people, new Student("樱木花道"));//addAll()相当于
        //ArrayList<Person> people = new ArrayList<>();
        //people.add( new Student("樱木花道") ) //Person可以接收Student对象 //多态
    }

    private static void method01() {
        //addAll基本使用
        ArrayList<String> al = new ArrayList<>();
        Collections.addAll(al, "张三", "李四", "王五");
        System.out.println(al);
    }
}
